﻿
// MFCDrawingBoardView.h: CMFCDrawingBoardView 类的接口
//

#pragma once

#include <memory>
#include <utility>
#include <list>
#include <unordered_map>

#include "CClassView.h"
#include "graphicShape/GraphicDefine.h"

class CGraphicShapeBase;
class InitOpenGL;
class CGraphFactor;

class CMFCDrawingBoardView : public CView
{
private:
	CClassView* m_pWndClassView = nullptr;

	HGLRC m_hRC;
	std::unique_ptr<CClientDC> m_pDC;
	std::unique_ptr<InitOpenGL> m_pInitOpengl;
	std::unique_ptr<CGraphFactor> m_pGraphFactor;

	void reset();

	ShapeEnum m_emShape = SE_NONE;

	// 拖动图形时,所选择的图形编号
	int m_nSelectedGraphID = 0;

	// 鼠标左键按下时的起始点
	CPoint m_startPoint;

	// 鼠标左键按下的移动过程中,上一次点的坐标
	CPoint m_prePoint;

	std::list<std::shared_ptr<CGraphicShapeBase>> m_listPoint;

	// 鼠标左键按下拖动图形时,要不挺修改图形的起点终点坐标,使用 std::unordered_map 查找内存中的图形坐标,要快很多
	std::unordered_map<int, std::shared_ptr<CGraphicShapeBase>> m_mapPoint;

	// 判断绘图状态
	bool m_bDrawing = false;
	// 橡皮擦状态
	bool m_bRubber = false;
	// 移动图形的状态
	bool m_bDragging = false;

	// 递增自然数
	int m_nInc = 1;

protected: // 仅从序列化创建
	CMFCDrawingBoardView() noexcept;
	DECLARE_DYNCREATE(CMFCDrawingBoardView)

// 特性
public:
	CMFCDrawingBoardDoc* GetDocument() const;

	void SetClassView(CClassView* pWndClassView) { m_pWndClassView = pWndClassView; }

// 操作
public:

// 重写
public:
	virtual void OnDraw(CDC* pDC);  // 重写以绘制该视图
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
	virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
	virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
	virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

// 实现
public:
	virtual ~CMFCDrawingBoardView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// 生成的消息映射函数
protected:
	afx_msg void OnFilePrintPreview();
	afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
	DECLARE_MESSAGE_MAP()
public:
	virtual void OnInitialUpdate();
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
private:
	float ChangePos(float num, int mode);
	void CustomDraw(const std::shared_ptr<CGraphicShapeBase>& pDrawGraphBase);
	void CustomDraw();
	void CustomDrawHistory();
	void DelGraph(const CPoint& point);

	// 把 curPoint 作为参照坐标点, 拖动图形
	void DragGraph(const CPoint& curPoint);

	// 判断是否能拖动
	bool CanDragGraph();

public:
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnDestroy();
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnHotKey(UINT nHotKeyId, UINT nKey1, UINT nKey2);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
private:
	void Clear();
};

#ifndef _DEBUG  // MFCDrawingBoardView.cpp 中的调试版本
inline CMFCDrawingBoardDoc* CMFCDrawingBoardView::GetDocument() const
   { return reinterpret_cast<CMFCDrawingBoardDoc*>(m_pDocument); }
#endif

